import 'package:example/shopper/cart.dart';
import 'package:example/shopper/catelog.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import './basic/counter_page.dart';
import './basic/inheried_page.dart';
import './shopper/cartlog_page.dart';
import './shopper/cart_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider(create: (context) => CatalogModel()),
        ChangeNotifierProxyProvider<CatalogModel, CartModel>(
          create: (context) => CartModel(), 
          update: (context, catalog, cart) {
            if (cart == null) throw ArgumentError.notNull('cart');
            cart.catalog = catalog;
            return cart;
          }
        )
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
        routes: {
          '/counter': (context) => const CounterPage(),
          '/inheriedWidget': (context) => const InheriedPage(),
          '/catelogPage': (context) => const MyCatalogPage(),
          '/cartPage': (context) => const MyCartPage(),
        },
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<String> pages = ['counter', 'InheriedWidget', 'catelog'];
  final List<String> routers = ['/counter', '/inheriedWidget', '/catelogPage'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: ListView.builder(
        itemCount: pages.length,
        itemBuilder: (context, index)  => Column(
          children: [
            ListTile(
              leading: CircleAvatar(
                backgroundColor: Colors.lightGreen,
                child: Text(pages.elementAt(index)[0])
              ),
              trailing: const Icon(
                Icons.arrow_forward_ios, 
                color: Color.fromRGBO(192, 192, 192, 0.5)
              ),
              title: Text(
                pages.elementAt(index), 
                style: const TextStyle(color: Colors.black87)
              ),
              onTap: () => Navigator.pushNamed(context, routers.elementAt(index)),
            ),
            const Divider(color: Color.fromRGBO(153, 153, 153, 0.3), height: 1),
          ]
        ),
      ),
    );
  }
}
